package com.codeape.number.ui.main.fragment.divination;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.codeape.number.R;
import com.codeape.number.databinding.FragmentDivinationBinding;

/**
 * 占卜Fragment
 *
 * @author LiuPeiming
 * Created at 2018/9/27 0:22
 */
public class DivinationFragment extends Fragment {

    private View mInflaterView;

    public FragmentDivinationBinding binding = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (null != mInflaterView && null != mInflaterView.getParent()) {
            ViewGroup parent = (ViewGroup) mInflaterView.getParent();
            parent.removeView(mInflaterView);
            return mInflaterView;
        }

        mInflaterView = inflater.inflate(R.layout.fragment_divination, container, false);

        binding = DataBindingUtil.bind(mInflaterView);
        assert binding != null;
        binding.setViewModel(new DivinationViewModel(this));

        binding.editText1.setCustomSelectionActionModeCallback(ForbidPaste());
        binding.editText2.setCustomSelectionActionModeCallback(ForbidPaste());
        binding.editText3.setCustomSelectionActionModeCallback(ForbidPaste());

        binding.editText1.addTextChangedListener(FocusSkip(binding.editText1, binding.editText2));
        binding.editText2.addTextChangedListener(FocusSkip(binding.editText2, binding.editText3));
        binding.editText3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if ((s.toString().length() == 3) && (binding.editText1.getText().toString().length() == 3) && (binding.editText2.getText().toString().length() == 3)) {
                    binding.getViewModel().toCalculationCommand.execute();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return mInflaterView;

    }

    /**
     * 多个EditText输入时自动跳转焦点
     */
    TextWatcher FocusSkip(final EditText CurrentEdit, final EditText NextEdit) {

        return new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                if ((CurrentEdit.getText().toString()).length() == 3) {
                    // 获取焦点
                    NextEdit.requestFocus();
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        };
    }

    /**
     * 屏蔽EditText长按出现复制黏贴菜单
     */
    Callback ForbidPaste() {
        return new Callback() {
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // 这里可以添加自己的菜单选项（前提是要返回true的）
                return false;// 返回false 就是屏蔽ActionMode菜单
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        };
    }

}